package com.sky.controller.admin;

import com.sky.dto.SetmealDto;
import com.sky.dto.SetmealPageDto;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ISetmealService;
import com.sky.vo.SetmealOverVo;
import com.sky.vo.SetmealPageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public Result<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
        //log.info("{},{}",setmealDto.getName(),setmealDto.getSetmealDishes().toString()); 测试成功
        setmealService.save(setmealDto);
        return Result.success("套餐添加成功");
    }

    /**
     * 套餐分页查询
     * @param setmealPageDto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<SetmealPageVo>> page(SetmealPageDto setmealPageDto){
       // log.info("{}",setmealPageDto.toString()); 测试成功
       PageResult<SetmealPageVo> pageResult= setmealService.page(setmealPageDto);
       log.info("{}",pageResult.toString());
        return Result.success(pageResult);
    }

    /**
     * 根据Id查询套餐以及关联的菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealOverVo> getById(@PathVariable(value = "id")Long id){
       // log.info("{}",id); 测试成功
        SetmealOverVo setmealOverVo=setmealService.getById(id);
        return Result.success(setmealOverVo);
    }

    /**
     * 修改套餐的状态，起售或者停售
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable(value = "status")Integer status,
                                 Long id){
        //log.info("{},{}",status,id); 测试成功
        setmealService.modifyStatus(status,id);
        return Result.success("修改套餐状态成功");
    }

    /**
     * 根据id删除套餐信息
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam(value = "ids") List<Long> ids){
       // log.info("{}",ids.toString()); 测试成功
        setmealService.deleteByIds(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    public Result<String> modifySetmealWithDishes(@RequestBody SetmealDto setmealDto){
        //log.info("{},{}",setmealDto.getImage(),setmealDto.getSetmealDishes()); 测试成功
        setmealService.modifySetmealWithDishes(setmealDto);
        return Result.success("修改套餐信息成功");
    }
}
